package net.lcheng.sellerCenter.controller;

import com.alibaba.fastjson.JSON;
import net.lcheng.model.User;
import net.lcheng.model.WxShop;
import net.lcheng.model.WxShopCategory;
import net.lcheng.service.UserService;
import net.lcheng.service.WxShopService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by LV on 2016/6/30.
 */
@Controller
public class WxShopController {

    @Autowired
    private UserService userService;
    @Autowired
    private WxShopService wxShopService;

    /**
     * 异步获取商店分类信息
     * @return
     */
    @RequestMapping(value = "/shop/getShopCategoryList")
    @ResponseBody
    public String getShopCategoryList(){
        //获取当前用户
        Subject currentUser= SecurityUtils.getSubject();
        if(currentUser.isAuthenticated()){
            String userPhoneNum=currentUser.getSession().getAttribute("UserPhoneNum").toString();
            //获取用户信息
            User user=userService.getUserByPhone(userPhoneNum);
            if (user!=null){
                //获取商店信息
                WxShop shop=wxShopService.getShopInforByUserId(user.getUserId());
                if (shop!=null){
                    //获取商店分类列表
                    List<WxShopCategory> list=wxShopService.getShopCategoryList(shop.getShopId());
                    return JSON.toJSONString(list);
                }
            }
        }
        return null;
    }
}
